// stores/setup.js选项式创建Store实例
import {computed, ref} from 'vue';
import {defineStore} from 'pinia';

/**
 * defineStore的用法说明
 *  - defineStore所创建的实例一般以useXxxStore命名
 *  - 第一个参数为必传的独一无二的名称，用于连接Store和Devtools
 *  - 第二个参数为返回被暴露属性和方法组成的对象的Setup函数，ref()、computed()和function()类似于Vue组件中的data、computed和methods
 */
export const useSetupStore = defineStore('setup', () => {
  const string = ref('SYX');
  const number = ref(10);
  const boolean = ref(true);
  const array = ref(['A', 'B', 'C']);
  const object = ref({name: 'SYX', age: 19});
  
  const encrypt = computed(() => '加密**' + string.value + '**加密');
  
  function increment() {
    number.value++;
  }
  async function asynchronous() {
    // async-await异步函数
    await new Promise(resolve => {
      console.log('异步Promise');
      resolve();
    });
  }
  
  return {string, number, boolean, array, object, encrypt, increment, asynchronous};
});